from pml.Parser.public import *

class ForIn(Node):
    def __init__(self, loopVariable=None, iter=None, body=None, bp=None, ep=None, depth=0):
        super().__init__('for-in', bp, ep, depth=depth)
        self.loopVariable = loopVariable
        self.iter = iter
        self.body = body
    def __repr__(self):
        depth = '\t' * self.depth
        return 'For-in({})(\n{}loopVariable={},\n{}iter={},\n{}body={}\n{})'.format(self.depth, depth, self.loopVariable, depth, self.iter, depth, self.body, '\t' * (self.depth - 1))

    def getObject(self):
        self.methods['__init__'] = self.tp_init

        return self

    @staticmethod
    def tp_init(frame, this, position):
        pass

class ForBuilder(ASTBuilder):
    def __init__(self, tokens=None, depth=0):
        super().__init__('For', tokens, depth)

    def getObject(self):
        self.methods['__init__'] = self.tp_init

        self.methods['buildFor'] = self.buildFor

        return self

    @staticmethod
    def buildFor(frame, this: 'ForBuilder', position):
        bp = this.token.bp
        this.getNextToken()
        if this.token.type != TokenConstants.TT_NAME:
            # error
            pass
        loopVariable = Objects.object.PMTypeObject(Name(this.token.value, this.token.bp, this.token.ep).getObject())
        this.getNextToken()
        if this.token.type != TokenConstants.TT_KEYWORD:
            # error
            pass
        keyword = this.token.value
        this.getNextToken()
        if keyword == TokenConstants.Keywords.IN:
            iterBuilder = Parser.Expression.ExpressionBuilder(this.tokens, depth=this.depth + 1)
            iter = iterBuilder.buildExpression(frame, iterBuilder, position)
            del iterBuilder
            this.updateToken()
            if this.token.type != TokenConstants.TT_KEYWORD or this.token.value != TokenConstants.Keywords.BEGIN:
                # error
                pass
            this.getNextToken()
            bodyBuilder = Parser.ASTBuilder(this.tokens, depth=this.depth + 1)
            body = bodyBuilder.buildAST(frame, bodyBuilder, position)
            ep = this.token.ep
            try:
                this.getNextToken()
            except StopIteration:
                pass
            return Objects.object.PMTypeObject(ForIn(loopVariable, iter, body, bp, ep, depth=this.depth).getObject())

    @staticmethod
    def tp_init(frame, this, position):
        pass
